package com.topone.controller;

import com.topone.Enum.CouponEnum;
import com.topone.Enum.CouponTypeEnum;
import com.topone.Enum.CouponspecialStatusEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.CouponCategoryVO;
import com.topone.VO.CouponInfoVO;
import com.topone.VO.ResultVO;
import com.topone.dataObject.CouponCategory;
import com.topone.dataObject.TcCoupon;
import com.topone.dto.BussinessDTO;
import com.topone.dto.TcCouponDTO;
import com.topone.dto.TcUserDTO;
import com.topone.service.CouponCategoryService;
import com.topone.service.TcCouponService;
import com.topone.service.TcUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/12/6,0006.
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/buyer/coupon")
public class BuyerCouponController {
    @Autowired
    private TcCouponService tcCouponService;
    @Autowired
    private CouponCategoryService couponCategoryService;
    @Autowired
    private TcUserService tcUserService;

    @RequestMapping("/list")
    @Cacheable(cacheNames = "couponList",key = "coupon")
    public ResultVO list() {
    //1.查询所有上家的优惠券
    List<TcCoupon> couponList = tcCouponService.findUpAll(CouponEnum.UP.getCode());
    //2.查询类目
    List<Integer> categoryTypeList = couponList.stream().map(e -> e.getCouponType())
            .collect(Collectors.toList());
    List<CouponCategory> couponCategoryList = couponCategoryService
            .findByCouponTypeIn(categoryTypeList);

    //3.数据拼装
    List<CouponCategoryVO> resultVOList = new ArrayList<>();
    for (CouponCategory couponCategory:couponCategoryList){
        CouponCategoryVO couponCategoryVO=new CouponCategoryVO();
        couponCategory.setCouponType(couponCategory.getCouponType());
        couponCategory.setCategoryName(couponCategory.getCategoryName());
        List<CouponInfoVO> couponInfoVOList=new ArrayList<>();
        for(TcCoupon tcCoupon:couponList){
            if(tcCoupon.getCouponType().equals(couponCategory.getCouponType())){
                CouponInfoVO couponInfoVO=new CouponInfoVO();
                BeanUtils.copyProperties(tcCoupon,couponInfoVO);
                couponInfoVOList.add(couponInfoVO);
            }
        }
        couponCategoryVO.setCouponInfoVOList(couponInfoVOList);
        resultVOList.add(couponCategoryVO);

    }
       return ResultVOUntil.success(resultVOList);
}


  @GetMapping("/couponListByBussinessId")
    public  ResultVO<Map<String,Object>> couponList(@RequestParam("bussinessId") String bussinessId,
                                                    Map<String,Object> map){
        try {
            TcCouponDTO tcCouponDTO=new TcCouponDTO();
            tcCouponDTO.setBussinessId(bussinessId);
            tcCouponDTO.setCouponStatus(CouponEnum.UP.getCode());
           List<BussinessDTO> bussinessItemCouponList= tcCouponService.findAllByStatus(tcCouponDTO);
           map.put("bussinessItemCouponList",bussinessItemCouponList);
        }catch (FanZoneException e){
            log.error("【查询商家优惠券】失败,e={}",e);
            map.put("error",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【查询商家优惠券】成功={}",map);

        return ResultVOUntil.success(map);
  }

  @GetMapping("/allCoupon")
    public  ResultVO<Map<String,Object>> allCoupon(Map<String,Object> map){
        try {
           List<TcCouponDTO> couponList= tcCouponService.findAllCoupon(CouponEnum.UP.getCode());
           map.put("allCouponList",couponList);
        }catch (FanZoneException e){
            log.error("【优惠活动查询】失败,={}",e);
            map.put("error",e.getMessage());
            return ResultVOUntil.error(1,"优惠活动查询");
        }
         map.put("success","成功");
        log.info("【优惠活动查询】成功 {}",map);
        return ResultVOUntil.success(map);
  }

  @GetMapping("/discountCoupon")
    public ResultVO<Map<String,Object>> discountCoupon(Map<String,Object> map) {
      try {
          List<TcCouponDTO> DiscountCouponDTOList = tcCouponService
                  .findDiscountCoupon(CouponTypeEnum.DiscountCoupon.getCode());
          map.put("DiscountCouponDTOList", DiscountCouponDTOList);
      } catch (FanZoneException e) {
          log.error("【折扣中心】折扣券查询失败，{}", e.getMessage());
          return ResultVOUntil.error(1, e.getMessage());
      }
      log.info("【折扣中心】折扣券查询成功，{}",map);
      return ResultVOUntil.success(map);
  }

    @GetMapping("/cashCoupon")
    public ResultVO<Map<String,Object>> cashCoupon(Map<String,Object> map){
        try {
            List<TcCouponDTO> cashCouponDTOList=tcCouponService
                    .findCashCoupon(CouponTypeEnum.CashCoupon.getCode());
            map.put("cashCouponDTOList",cashCouponDTOList);
        }catch (FanZoneException e){
            log.error("【优惠活动】代金券查询失败，{}",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【优惠活动】代金券查询成功，{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/otherCoupon")
    public ResultVO<Map<String,Object>> otherCoupon(Map<String,Object> map,@RequestParam("couponTypeId") Integer couponTtypeId){
        try {
            List<TcCouponDTO> otherCouponDTOList=tcCouponService
                    .findDiscountCoupon(couponTtypeId);
            map.put("otherCouponDTOList",otherCouponDTOList);
        }catch (FanZoneException e){
            log.error("【其他类型优惠券查询】查询失败，{}",e.getMessage());
            e.printStackTrace();
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【其他类型优惠券查询】查询成功，{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/freeCoupon")
    public ResultVO<Map<String,Object>> freeCoupon(Map<String,Object> map){
        try {
            List<TcCouponDTO> freeCouponList=tcCouponService.freeCoupon(
                    CouponspecialStatusEnum.welfareCoupon.getCode());
            map.put("freeCouponList",freeCouponList);
        }catch (FanZoneException e){
            log.error("【福利专区】查询免费优惠券失败,{}",e.getMessage());
            e.printStackTrace();
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【福利专区】查询免费优惠券成功,{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/flashSaleCoupon")
    public ResultVO<Map<String,Object>> flashSaleCoupon(Map<String,Object> map){
        try {
            List<TcCouponDTO> flashSaleCouponList= tcCouponService.findFlashSaleCoupon(
                    CouponspecialStatusEnum.FlashSaleCoupon.getCode());
            map.put("flashSaleCouponList",flashSaleCouponList);
        }catch (FanZoneException e){
            log.error("【限时抢购】查询限时抢购优惠券失败,{}",e.getMessage());
            e.printStackTrace();
            return ResultVOUntil.error(1,e.getMessage());
        }

        log.info("【限时抢购】查询限时抢购优惠券成功,{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/dealCouponOvertime")
    public ResultVO<Map<String,Object>> dealCouponOvertime(Map<String,Object> map){
        List<TcCouponDTO> overTimeCouponList;
        try {
            overTimeCouponList=tcCouponService.dealCouponOverTime();
        }catch (FanZoneException e){
            log.error("【处理超时优惠券】失败",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }
        map.put("success","共处理:"+overTimeCouponList.size());
        log.info("【处理超时优惠券】成功,{}",overTimeCouponList);
        return ResultVOUntil.success(map);
    }


    @GetMapping("/buyCoupon")
    public ResultVO<Map<String,Object>> buyCoupon(@RequestParam("bussinessId") String bussinessId,
                                                  @RequestParam("couponId") String couponId,
                                                  @RequestParam("openid")String openid,
                                                  Map<String,Object> map){

        if(StringUtils.isEmpty(bussinessId) && StringUtils.isEmpty(couponId)){
            log.error("【下单前查询优惠券信息】失败 bussinessId={},couponId={}",bussinessId,couponId);
            return ResultVOUntil.error(1,"bussinessName和couponIdO参数为空");
        }
        try {
            List<TcCouponDTO> orderCouponList= tcCouponService.paymentCoupon(bussinessId,couponId);
            TcUserDTO tcUserDTO=tcUserService.findUserByOpenid(openid);
            map.put("orderCouponList",orderCouponList);
            map.put("tcUserDTO",tcUserDTO);
        }catch (FanZoneException e){
            log.error("【下单前查询优惠券信息】相关信息查询失败 e={}",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【下单前查询优惠券信息】相关信息查询成功,{}",map);
        return ResultVOUntil.success(map);
    }

    }

